﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class PeopleRelatedBO : ChurchDataBusinessBase
    {
        public List<PeopleRelationshipDisplay> GetListForDisplay(int peoplePK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            // First get all the peoplePKs that they are related to
            IEnumerable<PeopleRelated> tmp = from pr in oDB.PeopleRelateds
                                             where pr.ActiveInd == true && 
                                             (pr.FromPeopleFK == peoplePK
                                             || pr.ToPeopleFK == peoplePK)
                                             select pr;

            List<PeopleRelationshipDisplay> result = new List<PeopleRelationshipDisplay>();

            PeopleBO pBO = new PeopleBO();
            LKRelationshipBO lkR = new LKRelationshipBO();

            foreach (PeopleRelated pr in tmp)
            {
                int? prPeoplePK = null;
                int descriptionColumn = -1;

                if (pr.FromPeopleFK == peoplePK)
                {
                    prPeoplePK = pr.ToPeopleFK;
                    descriptionColumn = 2;
                }
                else
                {
                    prPeoplePK = pr.FromPeopleFK;
                    descriptionColumn = 1;
                }

                People drP = pBO.GetRecordByPK((int)prPeoplePK);
                LKRelationship drLKR = lkR.GetRecordByPK((int)pr.LKRelationshipFK);

                PeopleRelationshipDisplay newRec = new PeopleRelationshipDisplay();
                newRec.PeoplePK = drP.PeoplePK;
                newRec.PeopleRelatedPK = pr.PeopleRelatedPK;
                newRec.FullName = drP.FirstName + " " + drP.LastName;
                if (drP.Suffix != null && drP.Suffix.Length > 0) newRec.FullName += ", " + drP.Suffix;

                if (descriptionColumn == 1) newRec.Relationship = drLKR.DescriptionOne;
                else if (descriptionColumn == 2) newRec.Relationship = drLKR.DescriptionTwo;

                result.Add(newRec);
            }

            return result;
        }

        public void AddRelationship(int fromPeoplePK, int toPeoplePK, int lkRelationshipFK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            PeopleRelated dr = new PeopleRelated();
            oDB.PeopleRelateds.InsertOnSubmit(dr);

            dr.FromPeopleFK = fromPeoplePK;
            dr.ToPeopleFK = toPeoplePK;
            dr.LKRelationshipFK = lkRelationshipFK;
            dr.ActiveInd = true;
            dr.LastUpdTimestamp = DateTime.Now;
            dr.LastUpdUser = UserInformation.UserName;

            oDB.SubmitChanges();
        }

        public void RemoveRelationship(int peopleRelatedPK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            PeopleRelated dr = (from pr in oDB.PeopleRelateds
                               where pr.PeopleRelatedPK == peopleRelatedPK
                               select pr).First();

            dr.ActiveInd = false;
            
            oDB.SubmitChanges();
        }
    }

    public class PeopleRelationshipDisplay
    {
        public int PeoplePK { get; set; }
        public int PeopleRelatedPK { get; set; }
        public string FullName { get; set; }
        public string Relationship { get; set; }
    }
}
